package edu.npu.cv.interceptor;

import edu.npu.cv.constant.RequestAttributes;
import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.session.SessionContentHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zyj
 * @className AdminSignInInterceptor
 * @description 登陆拦截器
 * @date 2020年6月16日
 */
@Component
public class AdminSignInInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String uri = request.getRequestURI();
        String sessionId = request.getSession().getId();
        if (uri.startsWith(TemplatePath.S + TemplatePath.ADMIN)
                && SessionContentHolder.isExistSignInUser(sessionId) == false) {
            request.getSession().setAttribute(RequestAttributes.ERROR_MSG, RequestAttributes.MSG_SIGN_IN_AGAIN);
            response.sendRedirect(request.getContextPath() + TemplatePath.S + TemplatePath.ADMIN
                    + TemplatePath.S + TemplatePath.ADMIN_SIGN_IN);
            return false;
        } else {
            request.getSession().removeAttribute(RequestAttributes.ERROR_MSG);
            return true;
        }
    }

}
